package com.unicomcz.commision;

import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.unicomcz.ActionBase;

public class TotalAction extends ActionBase
{
    private List<TotalCommision> tc;
    private String acctMonth="";
    private double totalCharge = 0;
    private double totalCount = 0;
    private String artId;
    private String artTitle;
    
    /**
     * @return the artId
     */
    public String getArtId()
    {
        return artId;
    }
    /**
     * @param artId the artId to set
     */
    public void setArtId(String artId)
    {
        this.artId = artId;
    }
    /**
     * @return the artTitle
     */
    public String getArtTitle()
    {
        return artTitle;
    }
    /**
     * @param artTitle the artTitle to set
     */
    public void setArtTitle(String artTitle)
    {
        this.artTitle = artTitle;
    }
    public String getAcctMonth()
    {
        return acctMonth;
    }
    public void setAcctMonth(String acctMonth)
    {
        this.acctMonth = acctMonth;
    }
    public List<TotalCommision> getTc()
    {
        return tc;
    }
    public void setTc(List<TotalCommision> tc)
    {
        this.tc = tc;
    }
    public String executeAction(){
        log = Logger.getLogger(this.getClass());      
        tc = new ArrayList<TotalCommision>();
        String sqlStr = "SELECT  decode(Mod(rownum,2),0,'table_row_even','table_row_odd') cssClas,t1.comm_id,t1.comm_name,nvl(t2.jf,0),nvl(t2.charge,0),nvl(t1.comm_action,'#'),decode(t1.brand,'GW','固网',t1.brand)  " + 
        		"FROM dls_code_comm t1,  " + 
        		"   (SELECT comm_month acct_month,comm_id comm_type,comm_money charge,0 jf FROM dls_total_comm WHERE comm_month=? AND depart_code=?) t2  " + 
        		"WHERE t1.comm_id=t2.comm_type(+)  and t1.list_id>=0  "+
        		" ORDER BY t1.list_id ";
        /*
        String sqlStr = "select decode(Mod(rownum,2),0,'table_row_even','table_row_odd') cssClass,acct_month,comm_type,jf,charge ";
        sqlStr += " from dls_total_yj where depart_id=? and acct_month=?";*/
        if (!query("dls",tc,sqlStr)) {
            //执行查询失败
            return "fail";
        }
        //读取新闻
        String newStr = "SELECT * FROM dls_article WHERE status=0  ORDER BY oper_time DESC";
        setupConn("dls");
        String[] news = getSingle(newStr);
        if( (news!=null)&&(news.length>2)){
            artId=news[0];
            artTitle = news[1];
        }
        return SUCCESS;
    }
    protected void regQueryParam() throws Exception{
        if (acctMonth.length()==0) {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
            acctMonth=sf.format(new java.util.Date());
        }      
        
        pstmt.setString(1, acctMonth);
        pstmt.setString(2, userInfo.getUserId());
    }
    protected void rs2List(ResultSet rs,List out) throws Exception{
        /*
         * 1.发展佣金
         * 2.达量佣金
         * 3.积分补贴
         * 4.空充补贴
         * 5.话费提成
         * 6.首充佣金
         * 7.3G第五月佣金
         * 8.终端匹配奖
         */
        TotalCommision t = new TotalCommision();
        String url = "#";
        t.setCssClass(rs.getString(1));
        t.setAcctMonth(acctMonth);
        t.setComId(rs.getString(2));
        t.setComName(rs.getString(3));
        t.setCount(rs.getString(4));
        t.setCharge(rs.getString(5));
        totalCount += rs.getDouble(4);
        totalCharge += rs.getDouble(5);
        /*
        if (rs.getString(3).compareTo("发展佣金") ==0 ) {
            url = "/commision/executedetailFz.do?acctMonth="+acctMonth;
        }
        if (rs.getString(3).compareTo("达量佣金") ==0 ) {
            //TODO 增加达量佣金
            url = "detailDl.do#?acctMonth="+acctMonth;
        }
        if (rs.getString(3).compareTo("空充补贴") ==0 ) {
          //TODO  空充补贴
            url = "#?acctMonth="+acctMonth;
        }
        if (rs.getString(3).compareTo("话费提成") ==0 ) {
            url = "/commision/detailTc.do?acctMonth="+acctMonth;
        }
        if (rs.getString(3).compareTo("首充佣金") ==0 ) {
            url = "/commision/detailSc.do?acctMonth="+acctMonth;
        }
        if (rs.getString(3).compareTo("3G第五月佣金") ==0 ) {
            //TODO 3G第五月佣金
            url = "#?acctMonth="+acctMonth;
        }
        if (rs.getString(3).compareTo("终端匹配奖") ==0 ) {
            //TODO 终端匹配奖
            url = "#?acctMonth="+acctMonth;
        }*/
        t.setUrl(rs.getString(6)+"&acctMonth="+acctMonth);
        t.setBrand(rs.getString(7));
        out.add(t);
    }
    protected void addTotal(List out) throws Exception{
        TotalCommision t = new TotalCommision();
        DecimalFormat format = (DecimalFormat) NumberFormat.getInstance();
        format.applyPattern("0.00");
        t.setAcctMonth("");
        t.setComId("合计");
        t.setComName("合计");
        t.setCount(format.format(totalCount));
        t.setCharge(format.format(totalCharge));
        t.setUrl("#");
        out.add(t);
    }
    protected void regParam2single() {
        
    }
}
